package com.ruoyi.project.relation.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.project.relation.constants.LoggerConstants;
import com.ruoyi.project.relation.domain.ao.SyncActionRequest;
import com.ruoyi.project.relation.domain.ao.SyncActonResponse;
import com.wb.business.config.BusinessEntityManager;
import org.shoukaiseki.aqoeous.config.Validator;
import org.shoukaiseki.aqoeous.model.ResultModel;
import org.shoukaiseki.common.exception.MessageVirtualException;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 **/
@RestController
@RequestMapping()
public class BusinessEntityController {

    final static Logger logger= LoggerConstants.SYNC_ACTION_LOGGER;

    @Autowired
    private BusinessEntityManager businessEntityManager;

    @Resource
    private Validator validator;

    /**
     * 主要用于查询,短时间内允许重复调用接口
     */
    @PostMapping("/app/business/appSyncActionQuery/{alias}")
    @ResponseBody
    public String appSyncActionQuery(@PathVariable("alias")String alias,@RequestBody JSONArray jsonArray, BindingResult bindingResult)
    {
        return syncAction(alias,jsonArray,bindingResult);
    }

    /**
     * 主要用于app变更表数据,短时间内不允许重复调用
     */
    @RepeatSubmit
    @PostMapping("/app/business/appSyncAction/{alias}")
    @ResponseBody
    public String appSyncAction(@PathVariable("alias")String alias,@RequestBody JSONArray jsonArray, BindingResult bindingResult)
    {
        return syncAction(alias,jsonArray,bindingResult);
    }

    /**
     * 批量更新
     */
    //@PreAuthorize("@ss.hasPermi('relation:OneWayBusinessRelationship:add')")
    @RepeatSubmit
//    @Log(title = "批量更新", businessType = BusinessType.INSERT)
    @PostMapping("/business/syncAction/{alias}")
    @ResponseBody
    public String syncAction(@PathVariable("alias")String alias,@RequestBody JSONArray jsonArray, BindingResult bindingResult)
    {
        logger.debug("alias={}",alias);
        List<SyncActionRequest> syncActionRequestList= null;
        if(jsonArray!=null){
            syncActionRequestList = jsonArray.toJavaList(SyncActionRequest.class);
        }
        this.validate(syncActionRequestList,bindingResult);

        ResultModel resultModel = new ResultModel();
        if(syncActionRequestList==null){
            return JSONObject.toJSONStringWithDateFormat(resultModel, "yyyy-MM-dd HH:mm:ss");
        }

        Map<String,SyncActonResponse> data = businessEntityManager.syncAction(syncActionRequestList);
        resultModel.setData(data);
        return JSONObject.toJSONStringWithDateFormat(resultModel, "yyyy-MM-dd HH:mm:ss");
    }

    void validate(List<SyncActionRequest> list, BindingResult bindingResult){
//        validator.validate(list,bindingResult);
        if (list==null||list.isEmpty()) {
            throw new MessageVirtualException("list 不能为空");
        }
        for (SyncActionRequest syncActionRequest: list) {
            validator.validate(syncActionRequest,bindingResult);
        }
    }
}
